package com.example.book_test.advice;

import com.example.book_test.model.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@Slf4j
@ControllerAdvice
@ResponseBody
public class ErrorHandler {
    @ExceptionHandler
    public Result exception(Exception e){
        log.error("发生异常, e:{}", e);
        return Result.fail("内部错误");
    }

    @ExceptionHandler
    public Result exception(NullPointerException e){
        log.error("发生异常, e:{}", e);
        return Result.fail("空指针错误");
    }

    @ExceptionHandler
    public Result exception(ArithmeticException e){
        log.error("发生异常, e:{}", e);
        return Result.fail("算术异常");
    }
}
